package filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
public class CorsFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        res.setHeader("Access-Control-Allow-Origin","*");//允许任何域名访问
        res.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS");//允许请求的访问类型
        res.setHeader("Access-Control-Max-Age","3600");
        res.setHeader("Access-Control-Allow-Headers","*");//允许的请求头
        res.setHeader("Access-Control-Allow-Credentials","false");//不允许携带cookie
        chain.doFilter(req,res);
    }
}
